home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -in_the_mag- / workbench / adf / adflibppc / demo / unadf.c < prev    next >
C/C++ Source or Header  |  2000-03-05  |  7KB  |  288 lines

  1.  
  2. /*
  3.  * unadf
  4.  *
  5.  */
  6.  
  7. #define UNADF_VERSION "0.5"
  8.  
  9.  
  10. #include<stdlib.h>
  11. #include<errno.h>
  12. #include<string.h>
  13.  
  14. #include "adflib.h"
  15.  
  16.  
  17.  
  18. void MyVer(char *msg)
  19. {
  20. }
  21.  
  22. void help()
  23. {
  24.     puts("unadf [-lrcs -v n] dumpname.adf");
  25.     puts("    -l : lists root directory contents");
  26.     puts("    -r : lists directory tree contents");
  27.     puts("    -c : use dircache data (must be used with -l)");
  28.     puts("    -s : display entries logical block pointer (must be used with -l)");
  29.     puts("    -v n : mount volume #n instead of default #0 volume");
  30. }
  31.  
  32. void printEnt(struct Volume *vol, struct Entry* entry, char *path, BOOL sect)
  33. {
  34. /*    long realSize=0;
  35.     char *realName=NULL;
  36. */
  37.     if (entry->type==ST_LFILE || entry->type==ST_LDIR || entry->type==ST_LSOFT)
  38.         return;
  39.     if (entry->type==ST_DIR)
  40.         printf("         ");
  41.     else
  42.         printf("%7ld  ",entry->size);
  43.  
  44.     printf("%4d/%02d/%02d  %2d:%02d:%02d ",entry->year, entry->month, entry->days,
  45.         entry->hour, entry->mins, entry->secs);
  46.     if (sect)
  47.         printf(" %06ld ",entry->sector);
  48.  
  49.     if (strlen(path)>0)
  50.         printf(" %s/",path);
  51.     else
  52.         printf(" ");
  53.     if (entry->type==ST_DIR)
  54.         printf("%s/",entry->name);
  55.     else
  56.         printf("%s",entry->name);
  57.     if (entry->comment!=NULL && strlen(entry->comment)>0)
  58.         printf(", %s",entry->comment);
  59.     putchar('\n');
  60.  
  61. }
  62.  
  63.  
  64. void printTree(struct Volume *vol, struct List* tree, char* path, BOOL sect)
  65. {
  66.     char *buf;
  67.     struct Entry* entry;
  68.  
  69.     while(tree) {
  70. //printf("pathtree=%s\n",path);
  71.         printEnt(vol, tree->content, path, sect);
  72.         if (tree->subdir!=NULL) {
  73.             entry = (struct Entry*)tree->content;
  74.             if (strlen(path)>0) {
  75.                 buf=(char*)malloc(sizeof(strlen(path)+1+strlen(entry->name)+1));
  76.                 if (!buf) {
  77.                     fprintf(stderr,"printTree : malloc error\n");
  78.                     return;
  79.                 }
  80.                 sprintf(buf,"%s/%s", path, entry->name);
  81.                 printTree(vol, tree->subdir, buf, sect);
  82.                 free(buf);
  83.             }
  84.             else
  85.                 printTree(vol, tree->subdir, entry->name, sect);
  86.         }
  87.         tree = tree->next;
  88.     }
  89. }
  90.  
  91.  
  92. void printDev(struct Device* dev)
  93. {
  94.     printf("Device : ");
  95.  
  96.     switch(dev->devType){
  97.     case DEVTYPE_FLOPDD:
  98.         printf("Floppy DD"); break;
  99.     case DEVTYPE_FLOPHD:
  100.         printf("Floppy HD"); break;
  101.     case DEVTYPE_HARDDISK:
  102.         printf("Harddisk"); break;
  103.     case DEVTYPE_HARDFILE:
  104.         printf("Hardfile"); break;
  105.     default:
  106.         printf("???"); break;
  107.     }
  108.  
  109.     printf(". Cylinders = %ld, Heads = %ld, Sectors = %ld",dev->cylinders,dev->heads,dev->sectors);
  110.  
  111.     printf(". Volumes = %d\n",dev->nVol);
  112. }
  113.  
  114.  
  115. void printVol(struct Volume* vol, int volNum)
  116. {
  117.     printf("Volume : ");
  118.     
  119.     switch(vol->dev->devType) {
  120.     case DEVTYPE_FLOPDD:
  121.         printf ("Floppy 880 KBytes,");
  122.         break;
  123.     case DEVTYPE_FLOPHD:
  124.         printf ("Floppy 1760 KBytes,");
  125.         break;
  126.     case DEVTYPE_HARDDISK:
  127.         printf ("HD partition #%d %3.1f KBytes,", volNum, (vol->lastBlock - vol->firstBlock +1) * 512.0/1024.0);
  128.         break;
  129.     case DEVTYPE_HARDFILE:
  130.         printf ("HardFile %3.1f KBytes,", (vol->lastBlock - vol->firstBlock +1) * 512.0/1024.0);
  131.         break;
  132.     default:
  133.         printf ("???,");
  134.     }
  135.  
  136.     printf(" between sectors [%ld-%ld].",vol->firstBlock, vol->lastBlock);
  137.  
  138.     printf(" %s ",isFFS(vol->dosType) ? "FFS" : "OFS");
  139.     if (isINTL(vol->dosType))
  140.         printf ("INTL ");
  141.     if (isDIRCACHE(vol->dosType))
  142.         printf ("DIRCACHE ");
  143.  
  144.     printf(". Filled at %2.1f%%.\n", 100.0-
  145.     (adfCountFreeBlocks(vol)*100.0)/(vol->lastBlock - vol->firstBlock +1) );
  146.  
  147. }
  148.  
  149.  
  150. int main(int argc, char* argv[])
  151. {
  152.     int i, j;
  153.     BOOL rflag, lflag, xflag, cflag, vflag, sflag;
  154.     char *filename, *devname;
  155.     char strbuf[80];
  156.  
  157.     struct Device *dev;
  158.     struct Volume *vol;
  159.     struct List *list, *cell;
  160.     int volNum;
  161.     BOOL true = TRUE;
  162.     
  163.     if (argc<2) {
  164.         help();
  165.         exit(0);
  166.     }
  167.  
  168.     rflag = lflag = xflag = cflag = vflag = sflag = FALSE;
  169.     filename = "";
  170.     devname = "";
  171.     volNum = 0;
  172.  
  173.     printf("unADF v%s : a unzip like for .ADF files, powered by ADFlib (v%s - %s)\n", 
  174.         UNADF_VERSION, adfGetVersionNumber(),adfGetVersionDate());
  175.  
  176.     /* parse options */
  177.     i=1;
  178.     while(i<argc) {
  179.         if (argv[i][0]=='-')
  180.             for (j=1; j<(int)strlen(argv[i]); j++)
  181.                 switch(argv[i][j]) {
  182.                 case 'v': 
  183.                     vflag = TRUE;
  184.                     if ((i+1)<(argc-1)) {
  185.                         i++;
  186.                         errno = 0;
  187.                         volNum = atoi(argv[i]);
  188.                         if (errno!=0 || volNum<0) {
  189.                             fprintf(stderr,"invalid volume number, aborting.\n");
  190.                             exit(1);
  191.                         }
  192.                     }
  193.                     else
  194.                         fprintf(stderr,"no volume number, -v option ignored.\n");
  195.                     break;
  196.                 case 'l': 
  197.                     lflag = TRUE;
  198.                     break;
  199.                 case 's': 
  200.                     sflag = TRUE;
  201.                     break;
  202.                 case 'c': 
  203.                     cflag = TRUE;
  204.                     break;
  205.                 case 'r':
  206.                     rflag = TRUE;
  207.                     break;
  208.                 case 'x':
  209.                     xflag = TRUE;
  210.                     break;
  211.                 case 'h':
  212.                 default:
  213.                     help();
  214.                     exit(0);
  215.                 }
  216.         else
  217.             /* the last non option string is taken as the filename */
  218.             if (i==(argc-1))
  219.                 if (strlen(devname)==0)
  220.                     devname = argv[i];
  221.                 else
  222.                     filename = argv[i];
  223.         
  224.         i++;
  225.     }
  226.  
  227.     putchar('\n');
  228.  
  229.     adfChgEnvProp(PR_VFCT, MyVer);
  230.  
  231.     /* initialize the library */
  232.     adfEnvInitDefault();
  233.  
  234.  
  235.     dev = adfMountDev( devname );
  236.     if (!dev) {
  237.         sprintf(strbuf,"Can't mount the dump device '%s'.\n", devname);
  238.         fprintf(stderr, strbuf);
  239.         adfEnvCleanUp(); exit(1);
  240.     }
  241.     printDev(dev);
  242.  
  243.     if (volNum>=dev->nVol) {
  244.         fprintf(stderr,"This device has only %d volume(s), aborting.\n",dev->nVol);
  245.         exit(1);
  246.     }
  247.  
  248.     vol = adfMount(dev, volNum, TRUE);
  249.     if (!vol) {
  250.         adfUnMountDev(dev);
  251.         fprintf(stderr, "Can't mount the volume\n");
  252.         adfEnvCleanUp(); exit(1);
  253.     }
  254.     printVol(vol, volNum);
  255.  
  256.     putchar('\n');
  257.     if (cflag && isDIRCACHE(vol->dosType)) {
  258.         adfChgEnvProp(PR_USEDIRC,&true);
  259.         puts("Using dir cache blocks.");
  260.     }
  261.  
  262.     if (lflag) {
  263.         if (!rflag) {
  264.             cell = list = adfGetDirEnt(vol,vol->curDirPtr);
  265.             while(cell) {
  266.                 printEnt(vol,cell->content,"", sflag);
  267.                 cell = cell->next;
  268.             }
  269.             adfFreeDirList(list);
  270.         } else {
  271.             cell = list = adfGetRDirEnt(vol,vol->curDirPtr,TRUE);
  272.             printTree(vol,cell,"", sflag);
  273.             adfFreeDirList(list);
  274.         }
  275.     }else if (xflag) {
  276.  
  277.     }
  278.     else
  279.         help();
  280.  
  281.     adfUnMount(vol);
  282.     adfUnMountDev(dev);
  283.  
  284.     adfEnvCleanUp();
  285.  
  286.     return(0);
  287. }
  288.